﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;

public partial class plateType : System.Web.UI.Page
{
    PlateType plate = new PlateType();
    //Inject log4net
    private static readonly ILog logger = LogManager.GetLogger(typeof(plateType).Name);
    protected void Page_Load(object sender, EventArgs e)
    {
        Inits();
        if (!Page.IsPostBack)
        {
            if (!string.IsNullOrEmpty(hdnId.Value))
                LoadUpdate(hdnId.Value);
        }
    }

    #region Private methods
    /// <summary>
    /// Init page
    /// </summary>
    /// <param name="id">string</param>
    private void Inits()
    {
        //lblErrorName.Visible = false;
        ////handle [Xoa] button
        //btnReset.ImageUrl = "images/xoa.gif";
        //btnReset.Attributes.Add("onmouseover", "this.src='images/xoa_hover.gif'");
        //btnReset.Attributes.Add("onmouseout", "this.src='images/xoa.gif'");

        ////handle [Save] button
        //btnSave.Attributes.Add("onmouseover", "this.src='images/save_hover.gif'");
        //btnSave.Attributes.Add("onmouseout", "this.src='images/save.gif'");

        ////page title
        //Page.Title = Constants.TITLE_SYSTEM + Constants.SPACE + Constants.SEPARATE + Constants.SPACE + lblTitle.Text;
        if (string.IsNullOrEmpty(hdnId.Value))
        {
            txtName.Enabled = txtPrice.Enabled = txtDes.Enabled = btnSave.Enabled= false;
        }
        else
        {
            txtPrice.Enabled = txtDes.Enabled = btnSave.Enabled = true;
        }

        LoadToGrid();
    }

    /// <summary>
    /// LoadUpdate
    /// </summary>
    /// <param name="id">Guid</param>
    private void LoadUpdate(string id)
    {
        //handle [Save] button
        btnSave.ImageUrl = "images/edit.jpg";
        btnSave.Attributes.Add("onmouseover", "this.src='images/edit_hover.jpg'");
        btnSave.Attributes.Add("onmouseout", "this.src='images/edit.jpg'");

        PlateType plat = null;
        try
        {
            plat = plate.GetById(int.Parse(id));
            //Load to control
            txtName.Text = plat.Name;
            txtDes.Text = plat.Description;
            txtPrice.Text = plat.Price.ToString();
            hdnId.Value = id;
            txtPrice.Enabled = txtDes.Enabled = btnSave.Enabled = true;            
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }


    /// <summary>
    /// LoadToGrid
    /// </summary>
    private void LoadToGrid()
    {
        try
        {
            IList<PlateType> list = plate.GetAll();
            if (list != null)
            {
                rptPlate.DataSource = list;
                rptPlate.DataBind();
                lblInform.Text = "";
                rptPlate.Visible = true;
            }
            else
            {
                lblInform.Text = "Không có loại xe nào.";
                rptPlate.Visible = false;
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }

    /// <summary>
    /// InsertOrUpdate
    /// </summary>
    private void InsertOrUpdate()
    {
        

        //get Employee from session
        Employee emp = Utils.GetEmployeeSession();


        //init BO
        PlateType plateType = new PlateType();
        //set to BO  
        if (!String.IsNullOrEmpty(hdnId.Value))
            plateType.ID = int.Parse(hdnId.Value);
        plateType.Name = txtName.Text.Trim();
        plateType.Description = txtDes.Text.Trim();
        plateType.Price = int.Parse(txtPrice.Text.Trim());
        Employee em = new Employee();
        em.Id = emp.Id;
        plateType.LastUser = em;
        try
        {
            if (plateType.InsertOrUpdate(plateType))
            {
                LoadToGrid();//load grid again 
                SetControlBlank();
                hdnId.Value = "";
            }
        }
        catch (Exception ex)
        {
            lblMsg.Text = Constants.ERROR_MSG;
            if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
        }
    }


    

    /// <summary>
    /// Delete
    /// </summary>
    //private void Delete(string id)
    //{
    //    try
    //    {
    //        if (plate.Delete(int.Parse(id)))
    //        {
    //            LoadToGrid();
    //            SetControlBlank();
    //            hdnId.Value = "";
    //        }
    //    }
    //    catch (Exception ex)
    //    {
    //        //lblMsg.Text = Constants.ERROR_MSG;
    //        lblMsg.Text = "Loại xe này đã được sử dụng";
    //        //lblErrorName.Visible = true;
    //        if (logger.IsErrorEnabled) { logger.Error(ex.Message); }
    //    }
    //}

    /// <summary>
    /// Action: press delete and edit
    /// </summary>
    /// <param name="strCommandName"></param>
    /// <param name="strArgument"></param>
    private void Action(string strCommandName, string strArgument)
    {
        switch (strCommandName.ToUpper())
        {
            //press delete button
            //case "DELETE":
            //    Delete(strArgument);
            //    break;
            //press edit icon
            case "EDIT":
                LoadUpdate(strArgument);
                break;
            default:
                break;
        }
    }

    private void SetControlBlank()
    {
        txtName.Text = "";
        txtDes.Text = "";
        txtPrice.Text = "";
    }

    #endregion

    #region Events
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {

        InsertOrUpdate();
    }
    protected void btnReset_Click(object sender, ImageClickEventArgs e)
    {
        //txtName.Text = "";
        txtDes.Text = "";
    }
    protected void rptPlate_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        //get command
        string strCommandName = e.CommandName.Trim();
        string strArgument = e.CommandArgument.ToString().Trim();

        hdnId.Value = strArgument;
        Action(strCommandName, strArgument);
    }
    #endregion
    protected void rptPlate_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            PlateType plat = (PlateType)e.Item.DataItem;
            ImageButton btnEdit = (ImageButton)e.Item.FindControl("btnEdit");
            if (plat.ID.Equals(0))
            {
                btnEdit.Visible = false;
            }
            
        }
    }
}